//
//  TextEditingViewController.m
//  FunOCR
//
//  Created by Luke Du on 14/03/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "TextEditingViewController.h"
#import "FunOCRAppDelegate.h"
#import "GDataEntryDocBase.h"

@interface TextEditingViewController (PrivateMethods)
- (void) prepareSending;
- (void) finishedSending;
@end



@implementation TextEditingViewController

@synthesize tvEditor;
@synthesize bbiBack;
@synthesize bbiSave;
@synthesize content;

@synthesize spinner;


- (void)dealloc {
    [super dealloc];
	
	[tvEditor release];
	
	[bbiBack release];
	
	[bbiSave release];
	
	[content release];
	
	[spinner release];
}





// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	appDelegate = (FunOCRAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	[tvEditor becomeFirstResponder];
	
	tvEditor.text = self.content;
	
	
}





-(IBAction)saveClicked:(id)sender{
	
	if ([appDelegate isServerReachable]) {
		[self performSelectorOnMainThread:@selector(uploadFile) withObject:nil waitUntilDone:YES];
	}
	else {
		
		NSLog(@"No Internet Connection");
		
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Network Connection Error" 
							  message:@"You need to be connected to the internet to use this feature." 
							  delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}

	
	
	
	[self prepareSending];
}





- (IBAction)backClicked:(id)sender{
	
	NSLog(@"backClicked");
	
	[self dismissModalViewControllerAnimated:YES];
}





//upload the content of textviewer
- (void)uploadFile{
    
	NSData *uploadData = [tvEditor.text dataUsingEncoding:NSUTF8StringEncoding];
	
	NSLog(@"tvEditor.text :: %@", tvEditor.text);
	NSLog(@"[appDelegate.currentEntry ETag] :: %@", [appDelegate.currentEntry ETag]);
	
	[appDelegate.currentEntry setUploadData:uploadData];
	[appDelegate.currentEntry setUploadMIMEType:@"text/plain"];
	
	
	
	//insert the entry into the docList feed
	GDataServiceTicket *ticket;
	GDataServiceGoogleDocs *service = [appDelegate docsService];
	
	ticket = [service fetchEntryByUpdatingEntry:appDelegate.currentEntry
									   delegate:self 
							  didFinishSelector:@selector(uploadFileTicket:finishedWithEntry:error:)];
    [self setUploadTicket:ticket];
		

}




// upload finished callback
- (void)uploadFileTicket:(GDataServiceTicket *)ticket finishedWithEntry:(GDataEntryDocBase *)entry error:(NSError *)error {
	NSLog(@"finishedWithEntry :: %@", [[entry content] stringValue]);
	
	
	
	[self setUploadTicket:nil];
	
} 





- (void)setUploadTicket:(GDataServiceTicket *)ticket {
	[mUploadTicket autorelease];
	mUploadTicket = [ticket retain];
	
	[self finishedSending];
}




@end













@implementation TextEditingViewController (PrivateMethods)

- (void) prepareSending{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
	
	
	[spinner startAnimating];
	
}



- (void) finishedSending{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	
	[spinner stopAnimating];
	
}



@end
